package com.lyn.solitude.pluginhost.uitls

import android.app.Application
import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.MediaStore
import java.io.File
import java.io.FileInputStream
import java.security.MessageDigest


/**
 *
 * @author longyn 2024/05/23
 * @version 1.0.0
 */
object PluginUtil {

    @JvmStatic
    fun getApplicationInfo(path: String, application: Application): ApplicationInfo? {
        val packageManager = application.packageManager
        val packageInfo = packageManager.getPackageArchiveInfo(path, PackageManager.GET_META_DATA)
        packageInfo?.let {
            return it.applicationInfo.apply {

            }
        }
        return null
    }

    @JvmStatic
    fun getPluginNameResId(path: String, application: Application): Int {
        getApplicationInfo(path, application)?.let {
            return it.labelRes
        }
        return -1
    }

    @JvmStatic
    fun getPluginLogoResId(path: String, application: Application): Int {
        getApplicationInfo(path, application)?.let {
            return it.icon
        }
        return -1
    }

    @JvmStatic
    fun getPluginUUID(path: String, application: Application): String {
        getApplicationInfo(path, application)?.let {
            return it.metaData.getString("plugin_uuid", "")
        }
        return ""
    }

    @JvmStatic
    fun getPluginVersion(path: String, application: Application): Int {
        getApplicationInfo(path, application)?.let {
            return it.metaData.getInt("plugin_version", 0)
        }
        return 0
    }

    @JvmStatic
    fun getPluginMainClassName(path: String, application: Application): String {
        getApplicationInfo(path, application)?.let {
            return it.metaData.getString("main_plugin_class_name", "")
        }
        return ""
    }


    @JvmStatic
    fun getPluginFileMd5(file: File): String {
        try {
            // 创建一个MessageDigest实例
            val digest = MessageDigest.getInstance("MD5")
            // 创建一个FileInputStream来读取文件
            val fis = FileInputStream(file)
            // 创建一个字节缓冲区来存储文件数据
            val byteArray = ByteArray(1024)
            var bytesCount = 0
            // 读取文件数据并更新MessageDigest
            while (fis.read(byteArray).also { bytesCount = it } != -1) {
                digest.update(byteArray, 0, bytesCount)
            }
            // 关闭FileInputStream
            fis.close()
            // 获取字节数组格式的MD5摘要
            val bytes = digest.digest()
            // 将字节数组转换为十六进制格式
            val sb = StringBuilder()
            for (b in bytes) {
                sb.append(String.format("%02x", b))
            }
            // 返回MD5值
            return sb.toString()
        } catch (e: Throwable) {
            e.printStackTrace()
        }
        return ""
    }


    @JvmStatic
    fun getDownloadFiles(context: Context): List<String> {
        val downloadFiles: MutableList<String> = ArrayList()
        val collection: Uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
        } else {
            MediaStore.Files.getContentUri("external")
        }
        val projection = arrayOf(
            MediaStore.Files.FileColumns.DISPLAY_NAME,
            MediaStore.Files.FileColumns.DATA
        )
        val selection = MediaStore.Files.FileColumns.RELATIVE_PATH + "=?"
        val selectionArgs = arrayOf(
            Environment.DIRECTORY_DOWNLOADS + "/"
        )
        context.contentResolver.query(collection, projection, selection, selectionArgs, null)
            .use { cursor ->
                if (cursor != null) {
                    val nameColumn =
                        cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DISPLAY_NAME)
                    val dataColumn =
                        cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA)
                    while (cursor.moveToNext()) {
                        val filePath = cursor.getString(dataColumn)
                        downloadFiles.add(filePath)
                    }
                }
            }
        return downloadFiles
    }
}